﻿Imports System.Data
Imports System.Data.SqlClient
Imports Entidad

Public Class AlertaDocumentoClienteDatos

    Sub ingresarAlerta(ByVal pedido As String, ByVal usuario As String, ByVal mensaje As String, tipoDOC As String, cia As String, suc As String)
        Dim conex As New Conexion
        Dim coneccion As SqlConnection = conex.conectar()
        coneccion.Open()
        Dim query As String = "INSERT INTO ALER_Documento_Cliente(ALE_CIA,ALE_SUC, " &
                              "TDOC_Codigo,ALE_DCI_Codigo,ALE_UsuarioEnv,ALE_UsuarioRec, " &
                              "ALE_FecIngreso,ALE_FecLectura,ALE_Descripcion,ALE_Activo) " &
                              "VALUES(@cia,@suc,@tipo,@codigo,@emisor,null,getDate(),null,@men,'1')"
        Dim comando As New SqlCommand
        With comando
            .Connection = coneccion
            .CommandText = query
            .Parameters.AddWithValue("@cia", cia)
            .Parameters.AddWithValue("@suc", suc)
            .Parameters.AddWithValue("@tipo", tipoDOC)
            .Parameters.AddWithValue("@codigo", pedido)
            .Parameters.AddWithValue("@emisor", usuario)
            .Parameters.AddWithValue("@men", mensaje)
            .ExecuteNonQuery()
        End With
        coneccion.Close()
    End Sub

    Function leerAlertaXAsignadorAnulados(ByVal codAsig As String, tipoDOC As String) As DataSet
        Dim ds As New DataSet
        Dim conex As New Conexion
        Dim coneccion As New SqlConnection
        Try
            coneccion = conex.conectar()
            coneccion.Open()
            Dim query As String = "SELECT TOP 1 ALE_Codigo,ALE_Descripcion " &
                                  "FROM ALER_Documento_Almacen_Persona " &
                                  "WHERE PER_Codigo = '" & codAsig & "' AND TDOC_Codigo = '" & tipoDOC & "' " &
                                  "AND ALE_Activo <> '0' AND ALE_Tipo = '04' AND (ALE_Prioriza = '0' " &
                                  "OR ALE_Prioriza <> '0') " &
                                  "ORDER BY ALE_Codigo DESC"

            Dim adapter As SqlDataAdapter = New SqlDataAdapter(query, coneccion)
            adapter.Fill(ds)
        Catch ex As SqlException
            MsgBox("La conexión no está disponible.")
        Catch e As Exception
            MsgBox(e.Message)
        Finally
            coneccion.Close()
        End Try

        Return ds
    End Function

    Sub actualizarAlerta(ByVal aler As Alerta)
        Dim conex As New Conexion
        Dim coneccion As New SqlConnection
        Try
            coneccion = conex.conectar()
            coneccion.Open()
            Dim query As String = "UPDATE ALER_Documento_Almacen_Persona " &
                                  "SET ALE_USU_Recibe = @usurec, ALE_FEC_Lectura = getDate(), " &
                                  "ALE_Activo = '0' " &
                                  "WHERE ALE_Codigo = @alecodigo"

            Dim comando As New SqlCommand
            With comando
                .Connection = coneccion
                .CommandText = query
                .Parameters.AddWithValue("@usurec", aler.ALE_UsurioRec)
                .Parameters.AddWithValue("@alecodigo", aler.ALE_CODIGO)
                .ExecuteNonQuery()
            End With
        Catch ex As SqlException
            MsgBox("La conexión no está disponible.")
        Catch e As Exception
            MsgBox(e.Message)
        Finally
            coneccion.Close()
        End Try
    End Sub

End Class
